<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers;

use app\config\Status;
use app\models\Department;
use app\modules\v1\controllers\parent\ParentController;
use yii\data\ActiveDataProvider;

class DepartmentController extends ParentController
{
    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionCreate(){
        $params = \Yii::$app->request->getBodyParams();
        $department = new Department();
        $department->load($params,'');
        if($department->save()){
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $department
            ];

        }
        return [
            'bCode' => Status::FAIL,
            'bData' => $department->errors
        ];
    }

    public function actionIndex(){
        $params = \Yii::$app->request->getQueryParams();
        // 关系加条件
        $query = Department::find()
            ->leftJoin('{{%branch}} as b','b.id={{%department}}.branch_id' )
            ->leftJoin('{{%user}} as u', 'u.id = {{%department}}.user_id')
            ->with(['chargeUser', 'branch']);

        if (isset($params['department_id'])) {
            $query->andWhere(['department_id' => $params['department_id']]);
        }

        if (isset($params['branch_id'])) {
            $query->andWhere(['branch_id' => $params['branch_id']]);
        }
        $pageSize = 10;
        if(isset($params['pageSize'])){
            $pageSize = (int)$params['pageSize'];
        }
        return new ActiveDataProvider([
            // 使用with方法实现贪加载
            'query' => $query,
            // 分页信息
            'pagination' => [
                'pageSize' => $pageSize,
            ],
            // 排序信息
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);
    }

    /**
     * @return array
     * @throws \yii\base\InvalidConfigException
     */
    public function actionUpdate(){
        $params = \Yii::$app->request->getBodyParams();
        $id = (int)$params['id'];
        $department = Department::findOne($id);
        if($department) {
            $department->load($params,'');
//            $department->source_id = 1;
//            $department->type_id = 1;
//            $department->status = 1;
            if($department->save()){
                return [
                    'bCode' => Status::SUCCESS,
                    'bData' => $department
                ];
            }
            return [
                'bCode' => Status::FAIL,
                'bData' => $department->errors
            ];
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => ['msg'=>'找不到该ID的记录']
        ];

    }

    public function actionList() {
        $params = \Yii::$app->request->getQueryParams();
        $result = Department::find()->where(['name' => $params])->all(); //['设计一室', '设计二室', '设计三室', '技经室']
        return $result;
    }
}